package com.story.modular.app.controller;

import com.story.common.annotion.BussinessLog;
import com.story.common.annotion.Permission;
import com.story.common.constant.Dict;
import com.story.common.constant.factory.ConstantFactory;
import com.story.common.exception.BizExceptionEnum;
import com.story.common.exception.BussinessException;
import com.story.common.persistence.dao.IndexPictureMapper;
import com.story.common.persistence.model.IndexPicture;
import com.story.config.properties.UpyunProperties;
import com.story.core.base.controller.BaseController;
import com.story.core.log.LogObjectHolder;
import com.story.core.support.BeanKit;
import com.story.core.util.ToolUtil;
import com.story.core.util.UriUtil;
import com.story.modular.app.dao.IndexPictureDao;
import com.story.modular.app.service.UpyunService;
import com.story.modular.app.transfer.IndexPictureDto;
import com.story.modular.app.warpper.IndexPictureWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @decription
 * @Date 2017-12-28 14:07
 */
@Controller
@RequestMapping(value = "/indexPicture")
public class IndexPictureController extends BaseController{

    public static final String PREFIX = "/app/index/";

    private static final Long EXPRIRE_TIME = 3600L;

    @Autowired
    private IndexPictureDao indexPictureDao;

    @Autowired
    private IndexPictureMapper indexPictureMapper;

    @Autowired
    private UpyunService upyunService;

    @Autowired
    private UpyunProperties upyunProperties;

    /**
     * 进入首页
     * @return
     */
    @RequestMapping(value = "")
    public String index() {
        return PREFIX +"index.html";
    }

    /**
     * 获得列表
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list() {
        List<Map<String,Object>> indexPictureList = indexPictureDao.indexList();
        List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
        for (Map<String,Object> map : indexPictureList) {
            Map<String,Object> resultMap = new HashMap<String,Object>();
            resultMap.put("id",map.get("id"));
            resultMap.put("pictureUrl",upyunProperties.getDomain() + map.get("pictureUrl"));
            if((int)map.get("ageType") == 0) {
                resultMap.put("ageType","全部歌曲");
            }
            if((int)map.get("ageType") == 1) {
                resultMap.put("ageType","0-1岁");
            }
            if((int)map.get("ageType") == 2) {
                resultMap.put("ageType","1-3岁");
            }
            if((int)map.get("ageType") == 3) {
                resultMap.put("ageType","3-6岁");
            }
            resultList.add(resultMap);
        }
        return super.warpObject(new IndexPictureWarpper(resultList));
    }

    /**
     * 进入添加页面
     * @return
     */
    @RequestMapping(value = "/indexPictureAdd")
    public String toAdd() {
        return PREFIX +"index_add.html";
    }

    /**
     * 新增
     * @param indexPictureDto
     * @return
     */
    @BussinessLog(value = "新增图片", key = "ageType", dict = Dict.IndexPictureDict)
    @RequestMapping(value = "/add")
    @Permission
    @ResponseBody
    public Object add(IndexPictureDto indexPictureDto) {
        if(ToolUtil.isOneEmpty(indexPictureDto,indexPictureDto.getAgeType(),indexPictureDto.getPictureUrl())) {
            throw new BussinessException( BizExceptionEnum.REQUEST_NULL);
        }
        IndexPicture indexPicture = new IndexPicture();
        BeanKit.copyProperties(indexPictureDto,indexPicture);
        int count = indexPictureDao.countIndexByAge(indexPicture.getAgeType());
        if(count > 0) {
            return new BussinessException(BizExceptionEnum.AGE_TYPE_EXIST);
        }
        indexPicture.setPictureUrl(UriUtil.getUri(indexPicture.getPictureUrl().split("\\?")[0]));
        indexPictureMapper.insert(indexPicture);
        return super.SUCCESS_TIP;
    }

    /**
     * 进入首页修改页面
     * @param pictureId
     * @param model
     * @return
     */
    @RequestMapping(value = "/indexPictureUpdate/{pictureId}")
    public String toUpdate(@PathVariable(value = "pictureId") Integer pictureId, Model model) {
        IndexPicture indexPicture = indexPictureMapper.selectById(pictureId);
        String pictureUrl = indexPicture.getPictureUrl();
        indexPicture.setPictureUrl(upyunProperties.getDomain() + pictureUrl + "?_upt="+upyunService.getTokenSign( UriUtil.getUri(pictureUrl),EXPRIRE_TIME));
        model.addAttribute(indexPicture);
        model.addAttribute("iName", ConstantFactory.me().getCategoryName(pictureId));
        LogObjectHolder.me().set(indexPicture);
        return PREFIX + "index_update.html";
    }

    /**
     * 修改首页图片
     * @param indexPicture
     * @return
     */
    @BussinessLog(value = "修改图片", key = "ageType", dict = Dict.IndexPictureDict)
    @RequestMapping(value = "/update")
    @Permission
    @ResponseBody
    public Object update(IndexPicture indexPicture) {
        if(ToolUtil.isOneEmpty(indexPicture,indexPicture.getId(),indexPicture.getAgeType(),indexPicture.getPictureUrl())) {
            throw new BussinessException( BizExceptionEnum.REQUEST_NULL);
        }
        indexPicture.setPictureUrl(UriUtil.getUri(indexPicture.getPictureUrl().split("\\?")[0]));
        indexPictureMapper.updateById(indexPicture);
        return super.SUCCESS_TIP;
    }

}
